Interactive Graphics- Instacart

Author

Karol Orozco

Published

February 17, 2023

Gganimate

Show the code
library(gganimate) 

day_week <- orders %>%
    mutate(day = as.factor(order_dow)) %>%
    mutate(hour = as.factor(order_hour_of_day)) %>%
    group_by(day,hour) %>%
    dplyr::summarise(count = n()) %>%
    arrange(desc(count))


day_weekp <-day_week %>%
    ggplot(aes(x=day, y=hour))+
    geom_tile(aes(fill=count), colour = "white") + 
  
    scale_fill_gradient(name= "Number of\nOrders", low = "#fff1e6",high = "#00835C")+
  
    scale_x_discrete( position = "top",
                    breaks = c("0", "1", "2", "3", "4", "5", "6"),
                    label = c("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"),
                    expand=c(0,0))+
  
   scale_y_discrete( 
                    breaks = c("0", "6", "12", "18", "23"),
                    label = c("12am", "6am", "12pm", "6pm", "11pm"),
                    expand=c(0,0))+
  
      labs(title="Which Day and What Time\nDo Customers Order the Most?",
         x="", 
         y="",
         caption = "Maximum number of orders are placed between 9:00am and 4:00pm on Sunday and Monday. There is also a big number of orders during Firday\nand Saturday.")+
  
    theme_classic()+
  
    theme(
      
    axis.line=element_blank(),                                               
    axis.ticks=element_line(size=0.4),
    axis.text = element_text(size= 10, color= "#00835C"),
    axis.line.x = element_line(color= "#00835C" ),
    
    plot.background=element_blank(),         
    plot.title = element_text(size =10, face = "bold", hjust = 0.50, vjust = 1, colour = "darkgreen"),
    plot.caption = element_text(hjust = 0, size = 7, margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"), color = "#718c9e"),

    
    panel.grid = element_blank(),
    
    legend.position = "bottom",
    legend.title = element_text(size= 8),
    legend.margin=margin(grid::unit(0,"cm")),
    legend.key.width=grid::unit(2,"cm"),
    legend.key.height=grid::unit(0.2,"cm")
)

day_weekp+ transition_manual(day, cumulative = TRUE)

Plotly

Show the code
library(plotly)

type <- data   %>%
    group_by(product_id)%>% 
    dplyr::summarize(count = n()) %>% 
    top_n(20, wt = count) %>%
    left_join(select(products, product_id, product_name, category), by="product_id") %>%
    arrange(desc(count))

 

best <- type %>% 
ggplot(aes(x=reorder(product_name,count), y=count, color= category, text= paste0(product_name, ", Total Orders:", count)))+    geom_point(size= 2)+
    geom_segment(aes(x=reorder(product_name,count), 
                     xend=reorder(product_name,count), 
                     y=0, 
                     yend=count), size=0.8)+
  
    scale_y_continuous(labels = scales::comma) +
  
      labs(title="Bestsellers Products",
           subtitle = "Organic vs Non-Organic",
           y="", 
           x="", 
           legend = "")+
  
    scale_color_manual("", values = c("#FF8200", "#0AAD0A"))+

  
    theme_minimal()+
  
    theme(
       axis.text.x= element_text( size= 7),
       axis.text.y= element_text( size= 8),

       plot.title = element_text(hjust=0.5, size= 12, face = "bold", vjust = 0.5),
       plot.subtitle = element_text(hjust=0.5, size= 9, vjust = 0.5),
       
       panel.grid.major.x = element_blank(),
       panel.grid.major.y = element_blank(),
       panel.grid.minor.y = element_blank(),
       panel.grid = element_line(color = "#e5e5e5"))+
  
  
  
     coord_flip()

ggplotly(best, tooltip = "text")

Ggiraph

Show the code
library(ggiraph)
library(glue)


tooltip_css <- "background-color:#d8118c;color:white;padding:5px;border-radius:3px;"
bg_color <- "#D7E0DA"
font_color <- "#1f3225"


df3 <- data%>%
  select(product_id, reordered, product_name)%>%
  group_by(product_id, product_name)%>% 
  dplyr::summarize(proportion_reordered = mean(reordered), n=n())


gg_scatter <- ggplot(
  data = df3,
  mapping = aes(x=n, y=proportion_reordered,
                
    # here we add iteractive aesthetics
    tooltip = paste0(toupper(product_name), ":", 
                   n))) +
  geom_jitter_interactive(
    size = 3, hover_nearest = TRUE)+

  geom_smooth_interactive(se = FALSE, tooltip="smooth", data_id="smooth")+
  
  labs(y = "Proportion of reorders",
       title= "Association between number of orders and probability of reordering",
       caption = "Products with a high number of orders are naturally more likely to be reordered.\nHowever, there seems to be a ceiling effect.")+
  theme_minimal()+
  theme(text=element_text(family = "chivo", color="#1f3225"), 
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(color="#1f3225", size=0.1),
        axis.title=element_text(face="bold", size= 10),
        axis.text =element_text(color="#1f3225"),
        axis.title.y=element_text(margin=margin(r=10)),
        plot.title = element_text(size = 13, face = "bold", 
                                  hjust = 0.5, vjust = 0.5),
        plot.margin  = margin(20,20,20,20))

## Customizing girafe animations

girafe(
  ggobj = gg_scatter,
  bg = bg_color,
  options = list(
    opts_tooltip(css = tooltip_css, opacity = 1),
    opts_sizing(width = .7),
    opts_zoom(max = 1),
    opts_hover(
      css = girafe_css(
        css = glue("fill:{font_color};"),
        text = glue("stroke:none;fill:{font_color};fill-opacity:1;")))))

Reference

Instacart-market-basket-analysis by Jeremy Staley, Meg Risdal, sharathrao, Will Cukierski publisher by Kaggle, 2017. https://kaggle.com/competitions/instacart-market-basket-analysis